//
//  XZStudentListVC.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/10.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "XZStudentListVC.h"
#import "XZStudentContactCell.h"
#import "XZStudentDetailVC.h"

@interface XZStudentListVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@property (nonatomic, strong) NSMutableArray *dataSources;
@property (nonatomic, copy) NSString *searchStr;

@end

@implementation XZStudentListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nsnotificationAction:) name:@"zyStudent" object:nil];
    [self setupUI];
    [self setUpRefresh];
    self.dataSources  = [NSMutableArray array];
    
}


#pragma mark--- 搜索方法的实现
- (void)nsnotificationAction:(NSNotification *)nsnotification{
    
    self.searchStr = [NSString stringWithFormat:@"%@",[nsnotification valueForKey:@"object"]];
    NSLog(@"%@ %@",[nsnotification valueForKey:@"object"],nsnotification.userInfo);
    
    [self.tableView.mj_header beginRefreshing];
}



#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}

- (void)setNetWork{
    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=bindStudentsDetial&pageNumber=1
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=bindStudentsDetial&search=%@&pageNumber=%ld",self.searchStr.length != 0 ? self.searchStr:@"",(long)self.page];
    
     url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSources removeAllObjects];
            }
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
            
            NSArray *arr = [dict valueForKey:@"dataList"];
            
            
            for (NSDictionary *dic in arr) {
                
                ZYstudentDetailModel *model = [ZYstudentDetailModel new];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataSources addObject:model];
                
            }
            
            [self.tableView reloadData];

            
        }

        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
}


- (void)setupUI {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 100;
    _tableView.tableFooterView = [UIView new];
    _tableView.backgroundColor = RGBColor(245, 245, 245);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"XZStudentContactCell" bundle:nil] forCellReuseIdentifier:@"StudentClass"];
    [self.view addSubview:_tableView];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XZStudentContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StudentClass" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataSources[indexPath.row];
    cell.CallBack = ^(NSString *userID) {
        XZStudentDetailVC *detailVC = [[XZStudentDetailVC alloc] init];
        /// 详情页 删除的回调
        detailVC.callBack = ^{
            [self.tableView.mj_header beginRefreshing];
        };
        detailVC.userID = userID;
        [self.navigationController pushViewController:detailVC animated:YES];
    };
    cell.CallPhone = ^(NSString *phoneNum) {
        [self phoneChatNum:phoneNum];
    };
    return cell;
}

- (void)phoneChatNum:(NSString *)phoneNum{
    
    NSString *strs = phoneNum;
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",strs];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}


- (void)dealloc{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"zyStudent" object:nil];
    
}


@end
